﻿
using Furion;
using Microsoft.AspNetCore.HttpOverrides;
using NewLife;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.Ocsp;
using Org.BouncyCastle.Ocsp;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using WinForm.Model;
using static Org.BouncyCastle.Crypto.Engines.SM2Engine;

namespace WinForm.Application;
public class CommHelperUtil
{

    public static string windowsappshoptoken { get; set; } = "eyJ0IjoiM2xURVgxaWprdlQ1UjE1eENVZTBQN3M1bWNGTWozbnY5RzRWU3d0VXNyYm5BS0JMYWhCekhpRTd6WDE5MU5BeiIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9s";

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="AntiContent"></param>
    /// <returns></returns>

    public static string httpLoginGet(string url, string StrCookie, out string AntiContent)
    {
        string retString = "";
        AntiContent = "";
        try
        {

            HttpWebRequest request = null;
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json;";
            request.KeepAlive = false;
            request.AutomaticDecompression = DecompressionMethods.GZip;
            StrCookie = getNewCookie(StrCookie);
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", windowsappshoptoken);
            }
            else
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 ";
            }
            request.Headers.Add("Cookie", StrCookie);
            //if (StrCookie.Contains(";"))
            //{
            //    request.Headers.Add("Cookie", getNewCookie(StrCookie));
            //}
            //else
            //{
            //    List<System.Net.Cookie> cookiesList = new List<System.Net.Cookie>();
            //    cookiesList = StrCookie.FromJson<List<System.Net.Cookie>>();

            //    CookieContainer cookieContainer = new CookieContainer();
            //    foreach (var item in cookiesList)
            //    {
            //        if (!item.Name.Contains("mms_b84d1838"))
            //            cookieContainer.Add(item);

            //    }
            //    cookieContainer.Add(new Cookie("x-visit-time", getvisittime(), "/", "mms.pinduoduo.com"));
            //    request.CookieContainer = cookieContainer;

            //}
            if (url.Contains("commonMallInfo"))
            {
                request.Headers.Add("Referer", "https://mms.pinduoduo.com/mallcenter/changeAccountInfo/accountSetting/accountInfo");
            }
            else
            {
                request.Headers.Add("Referer", "https://mms.pinduoduo.com/home/");
            }

            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");
            // request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            request.Headers.Add("Accept-Encoding", "gzip,deflate");
            //request.Headers.Add("sec-ch-ua-mobile", "?0");
            //request.Headers.Add("Sec-Fetch-Site", "same-origin");
            //request.Headers.Add("Sec-Fetch-Mode", "cors");
            //request.Headers.Add("Sec-Fetch-Dest", "empty");
            //request.Headers.Add("sec-ch-ua-platform", "Windows");
            //request.Headers.Add("Upgrade-Insecure-Requests", "1");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string challenge = null;
            challenge = response.GetResponseHeader("cookie");
            if (challenge != null)
                AntiContent = challenge;
            retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myStreamReader = null;
            myResponseStream.Close();
            myResponseStream.Dispose();
            myResponseStream = null;
            request.Abort();
            request = null;

            // BuWindowpmmAPI(url, StrCookie, "", AntiContent);
            // 补环境
            // httpPostBuWindow(url, StrCookie, "", "");
            // 请求服务器获取sessionID
            // string JSESSIONID = BuWindowcheckSessioinID(url, StrCookie);
            // BuWindowcheckEventList(url, StrCookie, "", "");
        }
        catch (Exception ex)
        {

        }
        finally { }
        return retString;
    }

    public static string httpLoginTuiGuangGet(string url, string StrCookie)
    {
        string NewStrCookie = "";

        try
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            // request.AllowAutoRedirect = false; // 关闭自动重定向
            StrCookie = getNewCookie(StrCookie);
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", windowsappshoptoken);
            }
            else
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 ";
            }
            request.Headers.Add("Cookie", StrCookie);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.Redirect)
            {
                string location = response.Headers["Location"];

                // 创建新的请求到重定向的地址
                HttpWebRequest redirectRequest = (HttpWebRequest)WebRequest.Create(location);
                redirectRequest.CookieContainer = new CookieContainer(); // 使用CookieContainer来管理cookie

                // 将从前一个响应中获取的cookie复制到新请求
                foreach (Cookie cookie in response.Cookies)
                {
                    redirectRequest.CookieContainer.Add(cookie);
                }

                // 获取重定向后的响应
                HttpWebResponse redirectResponse = (HttpWebResponse)redirectRequest.GetResponse();
                StringBuilder cookieString2 = new StringBuilder();
                // 打印出重定向后的cookie
                foreach (Cookie cookie in redirectResponse.Cookies)
                {
                    Console.WriteLine("Cookie: " + cookie.Name + " = " + cookie.Value);
                    cookieString2.Append(cookie.Name + "=" + cookie.Value + ";");
                }
                NewStrCookie = cookieString2.ToString();
                // 关闭响应
                redirectResponse.Close();
            }
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string challenge = null;

            string retString = myStreamReader.ReadToEnd();
            // 关闭响应
            response.Close(); ;
        }
        catch (Exception ex)
        {

        }
        finally { }
        return NewStrCookie;
    }

    /// <summary>
    /// http 请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <returns></returns>
    public static string httpGet(string url, string StrCookie)
    {

        // 请求服务器获取sessionID
        string JSESSIONID = BuWindowcheckSessioinID(url, StrCookie);
        HttpWebRequest request = null;
        request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "GET";
        request.ContentType = "application/json";
        request.KeepAlive = false;
        if (StrCookie.Contains("windows_"))
        {
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
            request.Headers.Add("pddid", "fS5jylqF");
            request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZnUrZlVIdExDQXBqK2g5cDJvd20xNkw2eVVtZEo1VDNoMTFTdXZBMHRqdkFEOTdyMi8zR2lxRGpGTlZMZEM3cSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
        }
        else
        {
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 ";
        }
        if (!string.IsNullOrWhiteSpace(StrCookie))
            request.Headers.Add("Cookie", StrCookie);

        request.Headers.Add("Referer", "https://mms.pinduoduo.com/home/");
        request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");
        request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
        request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
        request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
        request.Headers.Add("sec-ch-ua-mobile", "?0");
        request.Headers.Add("Sec-Fetch-Site", "same-origin");
        request.Headers.Add("Sec-Fetch-Mode", "cors");
        request.Headers.Add("Sec-Fetch-Dest", "empty");
        request.Headers.Add("sec-ch-ua-platform", "Windows");

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

        string retString = myStreamReader.ReadToEnd();

        myStreamReader.Close();
        myStreamReader = null;
        myResponseStream.Close();
        myResponseStream.Dispose();
        myResponseStream = null;
        request.Abort();
        request = null;

        return retString;
    }



    /// <summary>
    /// http 请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <returns></returns>
    public static string httpGet2(string url, string StrCookie, string AntiContent)
    {
        HttpWebRequest request = null;
        request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "GET";
        request.ContentType = "application/json;charset=UTF-8";
        request.KeepAlive = false;
        if (StrCookie.Contains("windows_"))
        {
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
            request.Headers.Add("pddid", "fS5jylqF");
            request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZnUrZlVIdExDQXBqK2g5cDJvd20xNkw2eVVtZEo1VDNoMTFTdXZBMHRqdkFEOTdyMi8zR2lxRGpGTlZMZEM3cSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
        }
        else
        {
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 ";
        }
        if (!string.IsNullOrWhiteSpace(StrCookie))
            request.Headers.Add("Cookie", StrCookie);

        if (!string.IsNullOrWhiteSpace(AntiContent))
            request.Headers.Add("Anti-Content", AntiContent);
        request.Headers.Add("Referer", "https://mms.pinduoduo.com/home/");
        request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");
        request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
        // request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");

        request.Headers.Add("sec-ch-ua-mobile", "?0");
        request.Headers.Add("Sec-Fetch-Site", "same-origin");
        request.Headers.Add("Sec-Fetch-Mode", "cors");
        request.Headers.Add("Sec-Fetch-Dest", "empty");
        request.Headers.Add("sec-ch-ua-platform", "Windows");
        request.Accept = "application/json;charset=UTF-8";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

        string retString = myStreamReader.ReadToEnd();

        myStreamReader.Close();
        myStreamReader = null;
        myResponseStream.Close();
        myResponseStream.Dispose();
        myResponseStream = null;
        request.Abort();
        request = null;

        return retString;
    }



    /// <summary>
    /// http Post请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <returns></returns>
    public static async Task<string> httpPost(string url, string StrCookie, string postData, string AntiContent)
    {
        try
        {
            // 补环境
            httpPostBuWindow(url, StrCookie, postData, AntiContent);
            // 请求服务器获取sessionID
            string JSESSIONID = BuWindowcheckSessioinID(url, StrCookie);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "POST";
            request.ContentType = "application/json";
            if (!string.IsNullOrWhiteSpace(StrCookie))
                request.Headers.Add("Cookie", StrCookie);
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZnUrZlVIdExDQXBqK2g5cDJvd20xNkw2eVVtZEo1VDNoMTFTdXZBMHRqdkFEOTdyMi8zR2lxRGpGTlZMZEM3cSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
            }
            else
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");

            }
            //流式发起请求
            using (Stream requestStream = await request.GetRequestStreamAsync())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
                await requestStream.WriteAsync(dataBytes, 0, dataBytes.Length);
            }
            if (url.Contains("mms.pinduoduo.com"))
            {
                request.Headers.Add("Referer", "https://mms.pinduoduo.com/home/");

            }
            else if (url.Contains("yingxiao"))
            {
                request.Headers.Add("Referer", "https://yingxiao.pinduoduo.com/");
            }

            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");
            request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");

            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 循环获取流式数据
                while ((bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }

                return sb.ToString();
            }
        }
        catch (Exception e)
        {

            return e.Message;
        }
    }


    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="url">接口</param>
    /// <param name="imgPath">图片地址</param>
    /// <param name="uploadSign">签名</param>
    /// <param name="StrCookie"></param>
    /// <param name="AntiContent"></param>
    /// <returns></returns>
    public static string UploadImage(string url, string imgPath, string uploadSign, string StrCookie, string AntiContent)
    {
        string responseBody = string.Empty;

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "POST";


        if (!string.IsNullOrWhiteSpace(AntiContent))
            request.Headers.Add("Anti-Content", AntiContent);
        request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");

        if (StrCookie.Contains("windows_") || StrCookie.Contains("PddBrowser"))
        {
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
            request.Headers.Add("pddid", "fS5jylqF");
            request.Headers.Add("windows-app-shop-token", windowsappshoptoken);

            request.Headers.Add("Etag", "uRWgWvuYEPgIhynWDdKYFqVANZZpAXfh");
            request.Headers.Add("Accept", "*/*");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br, zstd");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");
            request.Headers.Add("Upgrade-Insecure-Requests", "1");
            request.Headers.Add("X-Long-Ab-Value", "1");
        }
        else
        {
            request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";

        }
        request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");

        request.Headers.Add("Origin", "https://mms.pinduoduo.com");
        request.Headers.Add("Cookie", StrCookie);
        request.Referer = getReferer(url);


        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Cookie", StrCookie);
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");

            // 创建MultipartFormDataContent实例
            using (MultipartFormDataContent form = new MultipartFormDataContent())
            {
                // 添加图片文件
                using (FileStream fileStream = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
                {
                    ByteArrayContent fileContent = new ByteArrayContent(fileStream.ReadBytes((int)fileStream.Length));
                    fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); // 根据图片类型设置
                    form.Add(fileContent, "image", Path.GetFileName(imgPath));
                }

                // 添加upload_sign字段
                form.Add(new StringContent(uploadSign), "upload_sign");

                // 发送POST请求
                try
                {
                    HttpResponseMessage response = client.PostAsync(url, form).Result;
                    response.EnsureSuccessStatusCode();
                    responseBody = response.Content.ReadAsStringAsync().Result;
                    //  responseBody;
                    // MessageBox.Show("Upload successful! Response: " + responseBody);
                }
                catch (HttpRequestException ex)
                {
                    // MessageBox.Show("Upload failed. Exception: " + ex.Message);
                }
            }
        }




        return responseBody;


        //// 定义分隔符
        //string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        //request.ContentType = "multipart/form-data; boundary=" + boundary;


        //// 构建请求体
        //byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
        //byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

        //// 文件部分
        //string filePartHeader = string.Format("Content-Disposition: form-data; name=\"iamge\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n", Path.GetFileName(imgPath));
        //byte[] fileHeaderBytes = Encoding.UTF8.GetBytes(filePartHeader);

        //// upload_sign参数部分
        //string signPartHeader = string.Format("Content-Disposition: form-data; name=\"upload_sign\"\r\n\r\n{0}\r\n", uploadSign);
        //byte[] signHeaderBytes = Encoding.UTF8.GetBytes(signPartHeader);

        //// 计算总长度
        //using (FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
        //{
        //    byte[] fileBytes = new byte[fs.Length];
        //    fs.Read(fileBytes, 0, fileBytes.Length);

        //    int totalLength = boundaryBytes.Length + fileHeaderBytes.Length + fileBytes.Length + boundaryBytes.Length + signHeaderBytes.Length + endBoundaryBytes.Length;
        //    request.ContentLength = totalLength;

        //    // 获取请求流
        //    using (Stream requestStream = request.GetRequestStream())
        //    {
        //        // 写入文件部分
        //        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
        //        requestStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
        //        requestStream.Write(fileBytes, 0, fileBytes.Length);

        //        // 写入upload_sign参数
        //        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
        //        requestStream.Write(signHeaderBytes, 0, signHeaderBytes.Length);

        //        // 结尾分隔符
        //        requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
        //    }
        //}

        //// 获取响应
        //using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        //{
        //    using (Stream responseStream = response.GetResponseStream())
        //    {
        //        using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
        //        {
        //            string responseText = reader.ReadToEnd();
        //            return responseText;
        //        }
        //    }

        //}

    }






    /// <summary>
    /// http Post请求
    /// </summary>
    /// <param name="url">接口地址</param>
    /// <param name="StrCookie">店铺Cookie</param>
    /// <param name="postData">json格式的参数</param>
    /// <param name="AntiContent">AntiContent</param>
    /// <returns></returns>
    public static string httpPost2(string url, string StrCookie, string postData, string AntiContent, long StoreId = 0)
    {
        string OldCookie = StrCookie;
        try
        {

            if (!url.Contains("templar/api/bill/pagingQueryMallBalanceBillListForMms") && url.Contains("/mains/promotionOverview") && !url.Contains("subSystem/generateAccessToken") && !url.Contains("yingxiao"))
            {
                StrCookie = getNewCookie(StrCookie);
            }



            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType = "application/json;charset=utf-8";
            byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = dataBytes.Length;
            if (string.IsNullOrWhiteSpace(StrCookie))
                return null;
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");
            if (StrCookie.Contains("windows_") || StrCookie.Contains("PddBrowser"))
            {
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", windowsappshoptoken);

                request.Headers.Add("Etag", "uRWgWvuYEPgIhynWDdKYFqVANZZpAXfh");
                request.Headers.Add("Accept", "*/*");
                request.Headers.Add("Cache-Control", "max-age=0");
                request.Headers.Add("Accept-Encoding", "gzip, deflate, br, zstd");
                request.Headers.Add("sec-ch-ua-mobile", "?0");
                request.Headers.Add("Sec-Fetch-Site", "same-origin");
                request.Headers.Add("Sec-Fetch-Mode", "cors");
                request.Headers.Add("Sec-Fetch-Dest", "empty");
                request.Headers.Add("sec-ch-ua-platform", "Windows");
                request.Headers.Add("Upgrade-Insecure-Requests", "1");
                request.Headers.Add("X-Long-Ab-Value", "1");
            }
            else
            {
                if (url.Contains("templar/api/bill/pagingQueryMallBalanceBillListForMms"))
                {
                    request.Headers.Add("Etag", "uRWgWvuYEPgIhynWDdKYFqVANZZpAXfh");
                    request.Headers.Add("Accept", "*/*");
                    request.Headers.Add("Cache-Control", "max-age=0");
                    request.Headers.Add("Accept-Encoding", "gzip, deflate, br, zstd");
                    request.Headers.Add("sec-ch-ua-mobile", "?0");
                    request.Headers.Add("Sec-Fetch-Site", "same-origin");
                    request.Headers.Add("Sec-Fetch-Mode", "cors");
                    request.Headers.Add("Sec-Fetch-Dest", "empty");
                    request.Headers.Add("sec-ch-ua-platform", "Windows");
                    request.Headers.Add("Upgrade-Insecure-Requests", "1");
                    request.Headers.Add("Priority", "\r\nu=1, i");
                    request.Headers.Add("Sec-ch-ua", "Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99");
                    request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                }
                else
                {
                    request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
                    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
                }


            }
            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");
            if (url.Contains("templar/api/bill/pagingQueryMallBalanceBillListForMms"))
            {
                request.Headers.Add("Origin", "https://cashier.pinduoduo.com");
            }
            else
            {
                request.Headers.Add("Origin", "https://mms.pinduoduo.com");
            }

            request.Headers.Add("Cookie", StrCookie);
            request.Referer = getReferer(url);


            //流式发起请求
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(dataBytes, 0, dataBytes.Length);
            }

            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 循环获取流式数据
                while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }
                return sb.ToString();
            }
        }
        catch (Exception e)
        {

            return e.Message;
        }
        finally
        {
        }
    }

    public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
    {
        var property = typeof(WebHeaderCollection).GetProperty("InnerCollection",
            System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (property != null)
        {
            var collection = property.GetValue(header, null) as NameValueCollection;
            collection[name] = value;
        }
    }


    /// <summary>
    /// POST 自动登录以后换取新的Cookie(拼多多推广平台)
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <param name="AntiContent"></param>
    /// <param name="NewCookie"></param>
    /// <returns></returns>
    public static string httpPostReturnCookie(string url, string StrCookie, string postData, string AntiContent, out string NewCookie)
    {
        try
        {
            NewCookie = "";
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "POST";
            request.ContentType = "application/json";
            if (!string.IsNullOrWhiteSpace(StrCookie))
                request.Headers.Add("Cookie", StrCookie);
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZnUrZlVIdExDQXBqK2g5cDJvd20xNkw2eVVtZEo1VDNoMTFTdXZBMHRqdkFEOTdyMi8zR2lxRGpGTlZMZEM3cSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
            }
            else
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");

            }
            if (url.Contains("mms.pinduoduo.com"))
            {
                request.Headers.Add("Referer", "https://mms.pinduoduo.com/home/");

            }
            else if (url.Contains("yingxiao"))
            {
                request.Headers.Add("Referer", "https://yingxiao.pinduoduo.com/");
            }

            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");
            request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");

            //流式发起请求
            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(dataBytes, 0, dataBytes.Length);
            }

            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 循环获取流式数据
                while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }

                string cookieHeader = response.Headers["Set-Cookie"];
                NewCookie = cookieHeader;

                return sb.ToString();
            }
        }
        catch (Exception e)
        {
            NewCookie = "";
            return e.Message;
        }
    }




    /// <summary>
    /// 补环境接口 正式操作之前补一次
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <param name="AntiContent"></param>
    /// <returns></returns>
    public static string httpPostBuWindow(string url, string StrCookie, string postData, string AntiContent)
    {
        try
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            if (url.Contains("mms/query/display/mall/goodsList"))
            {
                keyValuePairs.Add("path", "/goods/goods_list");
            }
            else if (url.Contains("mangkhut/mms/recentOrderList"))
            {
                keyValuePairs.Add("path", "/orders/list");
            }
            else if (url.Contains("vodka/v2/mms/batchOnSale"))
            {
                keyValuePairs.Add("path", "/orders/list");
            }
            else
            {
                keyValuePairs.Add("path", "/home/");
            }
            postData = keyValuePairs.ToJson();

            HttpWebRequest request = WebRequest.Create("https://mms.pinduoduo.com/newjersy/api/windows/popup") as HttpWebRequest;
            request.Method = "POST";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType = "application/json";
            request.ContentLength = postData.Length;
            if (string.IsNullOrWhiteSpace(StrCookie))
                return null;
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZmphVFo1eXAvWWpVY1lEb1l5cXUwbFhwOGJ4Q2NnZnY2aU9oQlhZMTBtNU1pOXJEQnBheTFlbENLNDhya1NvWSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            }
            else
            {
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
            }
            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");


            request.Headers.Add("Accept", "*/*");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");
            request.Headers.Add("Upgrade-Insecure-Requests", "1");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");
            if (url.Contains("mms.pinduoduo.com"))
            {
                request.Headers.Add("Origin", "https://mms.pinduoduo.com");
                request.Referer = getReferer(url);
            }
            else if (url.Contains("yingxiao"))
            {
                request.Headers.Add("Origin", "https://yingxiao.pinduoduo.com");
                request.Referer = getReferer(url);
                request.Headers.Add("Etag", "ob18wux5kf2ktu");
            }

            if (StrCookie.Contains(";"))
            {
                if (!string.IsNullOrWhiteSpace(StrCookie))
                    request.Headers.Add("Cookie", StrCookie + "x-visit-time=" + getvisittime() + ";mms_b84d1838=3523,321,3254,3532,3571,3474,3475,3477,3479,3482,1202,1203,1204,1205,3417,3397");
            }
            else
            {
                List<System.Net.Cookie> cookiesList = new List<System.Net.Cookie>();
                cookiesList = StrCookie.FromJson<List<System.Net.Cookie>>();
                CookieContainer cookieContainer = new CookieContainer();
                foreach (var item in cookiesList)
                {
                    if (!item.Name.Contains("mms_b84d1838"))
                        cookieContainer.Add(item);
                }
                cookieContainer.Add(new Cookie("x-visit-time", getvisittime(), "/", "mms.pinduoduo.com"));
                request.CookieContainer = cookieContainer;
            }


            //流式发起请求
            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(dataBytes, 0, dataBytes.Length);
            }

            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 循环获取流式数据
                while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }

                return sb.ToString();
            }
        }
        catch (Exception e)
        {

            return e.Message;
        }
        finally { }
    }


    /// <summary>
    /// 补环境 请求服务器 获取JSESSIONID
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <param name="AntiContent"></param>
    /// <returns></returns>
    public static string BuWindowcheckSessioinID(string url, string StrCookie)
    {
        string SessioinID = string.Empty;
        try
        {
            HttpWebRequest request = null;
            request = WebRequest.Create("https://mms.pinduoduo.com/pluto/whitelist/check") as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json;";
            request.KeepAlive = false;
            request.AutomaticDecompression = DecompressionMethods.GZip;
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZmphVFo1eXAvWWpVY1lEb1l5cXUwbFhwOGJ4Q2NnZnY2aU9oQlhZMTBtNU1pOXJEQnBheTFlbENLNDhya1NvWSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
            }
            else
            {
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 ";
            }
            if (!string.IsNullOrWhiteSpace(StrCookie))
            {
                if (StrCookie.Contains(";"))
                {
                    if (!string.IsNullOrWhiteSpace(StrCookie))
                        request.Headers.Add("Cookie", StrCookie + "x-visit-time=" + getvisittime() + ";mms_b84d1838=3523,321,3254,3532,3571,3474,3475,3477,3479,3482,1202,1203,1204,1205,3417,3397");
                }
                else
                {
                    List<System.Net.Cookie> cookiesList = new List<System.Net.Cookie>();
                    cookiesList = StrCookie.FromJson<List<System.Net.Cookie>>();

                    CookieContainer cookieContainer = new CookieContainer();
                    foreach (var item in cookiesList)
                    {
                        if (!item.Name.Contains("mms_b84d1838"))
                            cookieContainer.Add(item);
                    }
                    cookieContainer.Add(new Cookie("x-visit-time", getvisittime(), "/", "mms.pinduoduo.com"));
                    request.CookieContainer = cookieContainer;

                }

            }

            request.Headers.Add("Referer", "https://mms.pinduoduo.com/home/");
            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");
            request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            request.Headers.Add("Accept-Encoding", "gzip,deflate");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");
            request.Headers.Add("Upgrade-Insecure-Requests", "1");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");

            //CookieContainer cookieContainer = new CookieContainer();
            //cookieContainer.Add(new Cookie("name", "value", "/", "www.example.com"));
            //request.CookieContainer = cookieContainer;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string challenge = null;
            challenge = response.GetResponseHeader("Set-Cookie");
            if (challenge != null)
                SessioinID = challenge;
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myStreamReader = null;
            myResponseStream.Close();
            myResponseStream.Dispose();
            myResponseStream = null;
            request.Abort();
            request = null;

            if (!string.IsNullOrWhiteSpace(SessioinID))
            {
                string[] List = SessioinID.Split(';');
                SessioinID = List[0];
            }

        }
        catch (Exception e)
        {

        }
        finally { }

        return SessioinID;
    }






    /// <summary>
    /// 补环境
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <param name="AntiContent"></param>
    /// <returns></returns>
    public static string BuWindowcheckEventList(string url, string StrCookie, string postData, string AntiContent)
    {
        try
        {

            HttpWebRequest request = WebRequest.Create("https://mms.pinduoduo.com/earth/api/mms/top/banner/event/getEventList") as HttpWebRequest;
            request.Method = "POST";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType = "application/json";
            request.ContentLength = postData.Length;
            if (string.IsNullOrWhiteSpace(StrCookie))
                return null;
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZmphVFo1eXAvWWpVY1lEb1l5cXUwbFhwOGJ4Q2NnZnY2aU9oQlhZMTBtNU1pOXJEQnBheTFlbENLNDhya1NvWSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            }
            else
            {
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
            }
            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");


            request.Headers.Add("Accept", "*/*");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");
            request.Headers.Add("Upgrade-Insecure-Requests", "1");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");
            if (url.Contains("mms.pinduoduo.com"))
            {
                request.Headers.Add("Origin", "https://mms.pinduoduo.com");
            }
            else if (url.Contains("yingxiao"))
            {
                request.Headers.Add("Origin", "https://yingxiao.pinduoduo.com");

                request.Headers.Add("Etag", "ob18wux5kf2ktu");
            }
            request.Referer = getReferer(url);
            if (StrCookie.Contains(";"))
            {
                if (!string.IsNullOrWhiteSpace(StrCookie))
                    request.Headers.Add("Cookie", StrCookie + "x-visit-time=" + getvisittime() + ";mms_b84d1838=3523,321,3254,3532,3571,3474,3475,3477,3479,3482,1202,1203,1204,1205,3417,3397");
            }
            else
            {
                List<System.Net.Cookie> cookiesList = new List<System.Net.Cookie>();
                cookiesList = StrCookie.FromJson<List<System.Net.Cookie>>();
                CookieContainer cookieContainer = new CookieContainer();
                foreach (var item in cookiesList)
                {
                    if (!item.Name.Contains("mms_b84d1838"))
                        cookieContainer.Add(item);
                }
                cookieContainer.Add(new Cookie("x-visit-time", getvisittime(), "/", "mms.pinduoduo.com"));
                request.CookieContainer = cookieContainer;
            }


            //流式发起请求
            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(dataBytes, 0, dataBytes.Length);
            }

            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 获取头部信息
                WebHeaderCollection headers = response.Headers;

                // 遍历并打印头部信息
                foreach (var headerName in headers.AllKeys)
                {
                    //Console.WriteLine(headerName + ": " + headers[headerName]);
                    if (headerName.Contains("Set-Cookie"))
                    {
                        return headers[headerName];
                    }
                }


                // 循环获取流式数据
                while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }

                return sb.ToString();
            }
        }
        catch (Exception e)
        {

            return e.Message;
        }
        finally { }
    }


    /// <summary>
    /// 补环境模拟手表点击
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <param name="AntiContent"></param>
    /// <returns></returns>
    public static string BuWindowA2(string url, string StrCookie, string postData, string AntiContent)
    {
        try
        {
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("appKey", "fe");
            keyValuePairs.Add("data", "0aeJy9WumOHEdyBtTCQl4IwkJYCAYhCI3GGiCN6Zo86sjiwljPzWN4LGdE0hoQg7yquzjVVc2q6jlBP4-fxI_hHw3_MvwE_uUvq885JHE1nB0C3ZkRmREZkZERXyZ73PqmlCebspbj1n_fu-jIkUmLzsMOZb5HfO5HAYsCGpLI76x0dJEV5aYd1v3OQ-avdCpdWpu_slWRjeq0yDsPD2JCVqjvk3crHXks00yqzO5dHyYCNhtW2aoCca8uStmznYd1ObIrnazQMrtCS3NjT63ZVDNCMbS5W7qSFQYlMqtAHGajXppXUHLQ2eiXxcD-EyMvN7fx-Tq1J7aEIS-LsnYrA22z0KOBzWs0t4tyIOvOCibK4TBLtXSrXR2aBFPc57uVg05tT-tl0jtHbPSko8Hda3qW6rKoisRJ2TK9v4Ntdyz-jVVPUydCjdKs7qb5xKLPrcnFo1lHVB3NA6UuRrq_NxoOoQfRQlYm9OYTw5MirxFFHU0pTaiiKqERvhKOnqGWMuL-KI2FQCemkgrCOA2ET4il4BCqGSVCECqoIp15ZM5VToL42OamKKFopyh6mW0_zrXnzloTurNBuiiOUltt5c4hZkY9scqU6TH2ZWaTNGbd9uVxWixoJzg3xclalu2l5xB40elPD2AUBrDzBD13FGfn0YAtInyDQT5i1WVhRrreG-HYdWA0h2WRW6DMj2XVyJMV5nRU7EtCA0YDA1_5nImEcylpbHjEYmI7H5sV97KlOaGfGM4SonQUk4RYZpi1fkITwbgRxHdzRhIDnxXnaZbJ1cAj7ftvGouq9vP9NiUe-XMbhND_c_s09B-01xAVdhJVqwGPPB627z99tP9sd6WdpUe2vWP1UfGgPUkNq5RRj7h_7T2ZyDKdToF9RbUxHEEzmpnMe6MmEXXO-92N5yAZe5xq-8wOivIM_lqBQaU5kaXdKHI9Kkuba9BpiDBLB_a8yO2LJKksdr3rC7KgQuRalcrVvT509GXqHDscbWSycrGXF3V7nkabCJJ1ggMAFkzmDCQ7GNZnW8cy27V5zyVmzkEsy6Lc3l6EgHP76ybQ1nLzCg1b2isx177_OK9t9uDf157v7G5Neyvt5uv-qwftx2Vaue-3cGEph_1UV-375NQdAj9cCx-0N9PS6ppvUto-rg6DQ9IeNl8r7U1HfIDF9q00GfL9Rh-b8NqWzIXCUhS7eLs05gZ-muuil6c1qtTFx4_j1jdNqXg8RItMtnLc-t0oNePWn0Lu08hnohv5XMRB7Itx64fSutO3D_9XtRxg2j0akYhwHjIehxRioMpVpHHra-YxL4zGrW-H2P1DXRiL9gjeS9LcQsPvK23zK7TfIS1BzWBQdWVS27KbFTj149Z3Q1u6vZO5xoxXB1hoPPmLotAXLBBshXiUTP6oHyO3hJS9G7f-mFZuF0ps8rj1lTv60DJ8fzpufQFb7w2KUYU4PLaTKv6_XxxcdE47D30ar3QQgy7YEEsLIzmNP65MhjDRDHEjr4zhfDbGD5oxcXhtjD-X40cTOVFwbVDoXxnEousriuKPsPNbjXR-NLHiywMQflhymduwv81tX7-vHlmJvfijz2I_IgFyE5SU9sMIW_8YW_XTs-OY8TNx_jY6j9kw-enHnWJH9VlYRE_3hutHvckSWRxQKB23vq_kwK6XSD223Jc9CJBPTn7csipQ2SDXTzbevPrreXD0erS98zraN2Hw14UASjkMygppXi7HwX_-wwWSe57j5OzV0lWF2RQ_DkO4M0cy7zU17jo_oA4-Id24rONE3DgqxM4ltkatuy4ACkwxwOwa1XUXi7NmCwXp6loC4lOkldJWwyKv7DUucSkNgpowlVjHsb02GWyZ5rsoZKPhVm6urWMm_BIvwG5SxzNNcpkqJo7S7OI1kyIf1o5y5-fGkEYauURaCMGSnM1p3rtkTEynXhmgktTLljDiYO-yJTf79Kqu6wKuu_yK3Y3PLpuxLCVYOGVm4jSMrvgWDkGSvGemCAoLqhF236WLnQI3zd877-oyHdbV9PD9Y1Jija9s4krFuHXer-th9XB1FYnNG6IAjwALCk8Xg9VeUZhq8nmYpVX9l4ncp_bsuT35lyN7dog09TUq_obRyJlNTcISbAad0LFhk3HrP768mEOejktxrhDaBJ1RA-MGaZ6qLM2P5pTpwWig04QyEzgnDA1c0Wn0dVwIHOXFycZUyUWnHOWuCi_0qVLmplqaMS0ESxTUomMg4eeymTenHi33Grwy6xwCPmlbVVdIKNrLlDQpIXG2tE6h3HZ0XHVr99NefyuHWcOzx3lSYHf-7wtc00rdT2sMwtnHhFPhEItK63yiKvSXzXkOFLF2f911u3G8Oq35E21dAKDV2fXFdZyvC5VmiwvVAGUvmwCh4eu5R2jgSq0zd4nWgKmQhpFHfXINrzjM1nHBiJBQBfBSE9rTcPtDmjxuvLAUIUCue9jUHEtbUL-doTG44vuDOSA773dcqcQG7exOHfVfXxzcBHAw-A4hzgR5zhvtN25Bk-7Obps6-PoC11a0t_baDN2Z8x-49X-HQ4xrst1NG3gxNfmrLHXWfnkB5_1BT8cv2N8Y8zo1tlhQ2hPMv5EVlX0tMweGprjhhyUOfIuTt5mM8qZytLUj3n_Qvmgf5NKd4LZDPO_a0EqWpu0Xe3WJnHlZsHfDiImCJ3MF9ZR-s47vJxK2TpGZ8iWo852uqnUJYIAzMCfen8HE9bOXoNuyblw0D5I-svM29IL2P_cOOtvTJXizJbj7pS0HafP2UHkoJsDxCAz3_LE-SpD0kNqGZ9vw9QageY4TcJnbs_WU4QALmBvNfWiCraGgSfCnNdv0Elxc9tH8xTFo_RIf2h4PEPO_pmtgZYWU8GvqKuSTo_movb29-iyzm1ZnsmyghoebytStziuz67enS4vysZVZ11tmlK5MlM0DwZTrAQZD8RIBMtdqOF-Napf0trGabamtl8hBmp0tU1zZW-4DqNejChR3jZsYNROq5dDlQGyqlYMbR7iNmhh-I7tvkWBvZtXFelaon2G5nfjx1e7NXKTaCg5xessi2y_c3a95BLtx-ElqcHGbcGa0orktPlpe3GXWm-mkZ7Lue1IX1XJ7wajSfNGpZb7cZsud-SitynreXohdlmpPh0vNAZ11cNNZalKy3JnPwIqWmnOZ1YeF2qV1zlYGrCM3mzt35dkcQYdAtVPCVT52fDOtUHvOGjLYz2dYwUVlU7yv0GY1bJms55j6EvnSzX-ZcdMLwDJ__opwE7G6RB3I0333OvWySN0j1CUWIMv-2fDKhHmhvUycvIReok1ec26iuReeZTIuOc7SN0V51BzsBQf3zXKtN0kBC-r0IWulM4_2SZg7RwJJ1fuz43SVf-mEXmH-ei5sCuvVYZ5qsvQvDoGcl9Ihjrox7-ZBSCvmZXpqs2o2hH2CrutjblJ2fdSSNoCBeyDZ3MXl40FvBsvvNbB8A4ggr92jxz0eAmwJj3IgrxD4oBjV0PHE4Z_oojMqM_cU3llC712XTlPtAR0P8eUQvLvPrB7m7tV0wlzVfcDlapVSj4ciicKQJzqkQnOR2Cj23jt3zGRCAqKyd0ngoDqcSnLN0cAcpXW1iu9D1ziUw_TQodozL2HUBEQr79jdjQgjlBEe0-BQhjTy0fmtulwb0FVmh8_cpycZD_yILhQxXJIEDQ-txaUt1PSyopsdNbnqTLW9rxrHeYZyQ00SLEygNCBcHBIVRVb58rdIJqiL8LdIhNfsxWcVDhgqYmap1ncgnOEGYLmNWXgHwjnikfqa8vgOhDf9rrvCenANETo2d6DF9wwxQYCouYudpZ41ETFW3IX3I9-zAYlxbOjfJH0IDGwvCZYZEp_t9oqZdMghXRJ2iVj1TahIHPndiArW9ZPIdIWVuqsCnkSBCkOGU3xJPm4Y15PCaigFC5FcupqbuOsLq7oxJ6TLeCItM8Ikvv8pcrjyieVR1KUWa_SDIOxKn_ndUAlt_VBoKu2nePN6lp0-QTS5CijkEFCgNF2F22ZMuIx1HIY2UUoxcTv5wyI7c9eQqhsFOJtKUUEJE9ZXjMXRb5Mtnj97lBbH5_6Pa--z7vH77stq_-2qeyivVp0xn19qk2xv5YcTq4a4SXZJHAjNFCdI3Fz7KGlK_zbJ0zrZvFycACl5MVWMGJPwWAVxEBoqCLmVaGoJkwrlKaCQHVvUBIm6GCmjTXAryTEJJYlRYOANE3MeRAprJkhOIlK3kuwnVIrINx6ligSJ5VThrFCidCjYrSS7-u8u7Ixq3-Bg6FhGmllfR_STCvjPClY6CnhsJJyC80wUJfCwFIG2oTOBwNkmiLnvaRZIEhkJ0ygxjCHv-bczSUWKRyYRIg65hTqVRDwSCjiL6gTZL6BUhER7saA-iUNfOWjEEmFJcrvQ4kEQ-QgvIkwkLNeBT2MlGNfItlGk4WERSaQ7L3JxYag1NApCwbk29JPqx88HNUFQ-NoXgfGDKFEJi30cQkAyHigNxyci5mEsvCRgRnMltQj8wOcI0uB2-xwmNBIJk9yXTClpIxEwyhULVchMgnQgsLlWxR5lglADHMMpThwLKQni2-2zTmhsIqa1ZdqKQBqhjDEsimLsLmeucoY0tp4mkjAmuAziJHYJWtnb2Vy5553K4yThQaIkN5IrYZSEmlvWLXC63Hflmsok0UbLOOJcmFstl8U4fCwOvCAMYgYkIKSNAy2TEEv_nGXl0P3mxDyTeZpYYL7PKbmqenchd1II9eQ_j5xg9wOOonTP_AfvPo5bX20_3Xkybv1zuZtvv30kP2y_eLuz8Z6eHa0_X380bv1-NEoNHbcexTqMQ8bDrkAW6foRY11gmaDLTIgbnlA8ZGw6nLnhiEYqlMJIQYHIDOsqpMautCSysQllSIJx6-vJr1TGrX8tXzzqM__0w6sX_6Y2XogXTz6wM_6TCPNjtV1-eP3k_wGST7O-");
            keyValuePairs.Add("sign", "d42dcf93103d9f468035b8b1c7d772b0a15304d1");
            keyValuePairs.Add("timestamp", Convert.ToInt64(1707033623961));
            postData = keyValuePairs.ToJson();

            HttpWebRequest request = WebRequest.Create("https://xg.pinduoduo.com/xg/pfb/a2") as HttpWebRequest;
            request.Method = "POST";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType = "application/json";
            request.ContentLength = postData.Length;
            if (string.IsNullOrWhiteSpace(StrCookie))
                return null;
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZmphVFo1eXAvWWpVY1lEb1l5cXUwbFhwOGJ4Q2NnZnY2aU9oQlhZMTBtNU1pOXJEQnBheTFlbENLNDhya1NvWSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            }
            else
            {
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
            }
            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");


            request.Headers.Add("Accept", "*/*");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");
            request.Headers.Add("Upgrade-Insecure-Requests", "1");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");
            if (url.Contains("mms.pinduoduo.com"))
            {
                request.Headers.Add("Origin", "https://mms.pinduoduo.com");
                request.Referer = getReferer(url);
            }
            else if (url.Contains("yingxiao"))
            {
                request.Headers.Add("Origin", "https://yingxiao.pinduoduo.com");
                request.Referer = getReferer(url);
                request.Headers.Add("Etag", "ob18wux5kf2ktu");
            }

            if (StrCookie.Contains(";"))
            {
                if (!string.IsNullOrWhiteSpace(StrCookie))
                    request.Headers.Add("Cookie", StrCookie + "x-visit-time=" + getvisittime() + ";mms_b84d1838=3523,321,3254,3532,3571,3474,3475,3477,3479,3482,1202,1203,1204,1205,3417,3397");
            }
            else
            {
                List<System.Net.Cookie> cookiesList = new List<System.Net.Cookie>();
                cookiesList = StrCookie.FromJson<List<System.Net.Cookie>>();
                CookieContainer cookieContainer = new CookieContainer();
                foreach (var item in cookiesList)
                {
                    if (!item.Name.Contains("mms_b84d1838"))
                        cookieContainer.Add(item);
                }
                cookieContainer.Add(new Cookie("x-visit-time", getvisittime(), "/", "mms.pinduoduo.com"));
                request.CookieContainer = cookieContainer;
            }


            //流式发起请求
            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(dataBytes, 0, dataBytes.Length);
            }

            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 循环获取流式数据
                while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }

                return sb.ToString();
            }
        }
        catch (Exception e)
        {

            return e.Message;
        }
        finally { }
    }


    /// <summary>
    /// 补环境
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <param name="AntiContent"></param>
    /// <returns></returns>
    public static string BuWindowpmmAPI(string url, string StrCookie, string postData, string AntiContent)
    {
        try
        {
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("app", "100073");
            keyValuePairs.Add("biz_side", "merchant-frontend");
            keyValuePairs.Add("report_time_ms", getvisittime());
            keyValuePairs.Add("rand_num", 5786);
            keyValuePairs.Add("crc32", 1101394352);
            var common_tags = new
            {
                d = "",
                m = "",
                osV = "",
                uid = "63417428",
                did = "",
                mid = "743895948",
                runningAppId = "b5ef6125-638a-4302-c24c-df7349526e8b",
                releaseVersion = "2024-02-01_17:08:16_3ddb36eb",
                p = "3"
            };
            var datas = new
            {
                category = 1,
                type = 100,
                id_raw_value = "",
                timestamp = getvisittime2(),
                api_ratio = 3,
                tags = new
                {
                    network = 0,
                    code = 200,
                    conn = "1",
                    srcPageId = "-1",
                    vip = "",
                    method = "",
                    reportTime = getvisittime2()
                },
                lvalues = new
                {
                    rspT = new
                    {
                        values = new int[] { 81 }
                    },
                    rspP = new
                    {
                        values = new int[] { 92 }
                    },
                    reqP = new
                    {
                        values = new int[] { 2 }
                    }
                },
                extras = new { }
            };
            keyValuePairs.Add("common_tags", common_tags);
            postData = keyValuePairs.ToJson();

            HttpWebRequest request = WebRequest.Create("https://apm.pinduoduo.com/api/pmm/api") as HttpWebRequest;
            request.Method = "POST";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType = "application/json";
            request.ContentLength = postData.Length;
            if (string.IsNullOrWhiteSpace(StrCookie))
                return null;
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZmphVFo1eXAvWWpVY1lEb1l5cXUwbFhwOGJ4Q2NnZnY2aU9oQlhZMTBtNU1pOXJEQnBheTFlbENLNDhya1NvWSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            }
            else
            {
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
            }
            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");


            request.Headers.Add("Accept", "*/*");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");
            request.Headers.Add("Upgrade-Insecure-Requests", "1");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");
            if (url.Contains("mms.pinduoduo.com"))
            {
                request.Headers.Add("Origin", "https://mms.pinduoduo.com");
                request.Referer = getReferer(url);
            }
            else if (url.Contains("yingxiao"))
            {
                request.Headers.Add("Origin", "https://yingxiao.pinduoduo.com");
                request.Referer = getReferer(url);
                request.Headers.Add("Etag", "ob18wux5kf2ktu");
            }

            if (StrCookie.Contains(";"))
            {
                if (!string.IsNullOrWhiteSpace(StrCookie))
                    request.Headers.Add("Cookie", StrCookie + "x-visit-time=" + getvisittime() + ";mms_b84d1838=3523,321,3254,3532,3571,3474,3475,3477,3479,3482,1202,1203,1204,1205,3417,3397");
            }
            else
            {
                List<System.Net.Cookie> cookiesList = new List<System.Net.Cookie>();
                cookiesList = StrCookie.FromJson<List<System.Net.Cookie>>();
                CookieContainer cookieContainer = new CookieContainer();
                foreach (var item in cookiesList)
                {
                    if (!item.Name.Contains("mms_b84d1838"))
                        cookieContainer.Add(item);
                }
                cookieContainer.Add(new Cookie("x-visit-time", getvisittime(), "/", "mms.pinduoduo.com"));
                request.CookieContainer = cookieContainer;
            }


            //流式发起请求
            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(dataBytes, 0, dataBytes.Length);
            }

            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 循环获取流式数据
                while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }

                return sb.ToString();
            }
        }
        catch (Exception e)
        {

            return e.Message;
        }
        finally { }
    }




    /// <summary>
    /// 刷新 获取新的Cookie
    /// </summary>
    /// <param name="url"></param>
    /// <param name="StrCookie"></param>
    /// <param name="postData"></param>
    /// <param name="AntiContent"></param>
    /// <param name="StoreId"></param>
    /// <returns></returns>
    public static string PddrefreshToken(string url, string StrCookie, string postData, string AntiContent)
    {
        try
        {
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            if (StrCookie.Contains("windows"))
            {
                keyValuePairs.Add("platform", 0);
            }
            else
            {
                keyValuePairs.Add("platform", 0);
            }

            postData = keyValuePairs.ToJson();
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType = "application/json";
            request.ContentLength = postData.Length;
            if (string.IsNullOrWhiteSpace(StrCookie))
                return null;
            if (!string.IsNullOrWhiteSpace(AntiContent))
                request.Headers.Add("Anti-Content", AntiContent);
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            if (StrCookie.Contains("windows_"))
            {
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.5.58.0 Safari/537.36 PddWorkbench-Online PddBrowser pdd_windows_version/3.4.6.40";
                request.Headers.Add("pddid", "fS5jylqF");
                request.Headers.Add("windows-app-shop-token", "eyJ0IjoiZmphVFo1eXAvWWpVY1lEb1l5cXUwbFhwOGJ4Q2NnZnY2aU9oQlhZMTBtNU1pOXJEQnBheTFlbENLNDhya1NvWSIsInYiOjEsInMiOjQsIm0iOjc0Mzg5NTk0OCwidSI6NjM0MTc0Mjh9");
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
            }
            else
            {
                request.Headers.Add("Etag", "rOHh24xqROYbCO8OJq2y3Z86nvbFrqVJ");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
            }
            request.Headers.Add("Sec-Ch-Ua", "Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121");


            request.Headers.Add("Accept", "*/*");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
            request.Headers.Add("sec-ch-ua-mobile", "?0");
            request.Headers.Add("Sec-Fetch-Site", "same-origin");
            request.Headers.Add("Sec-Fetch-Mode", "cors");
            request.Headers.Add("Sec-Fetch-Dest", "empty");
            request.Headers.Add("sec-ch-ua-platform", "Windows");
            request.Headers.Add("Upgrade-Insecure-Requests", "1");
            SetHeaderValue(request.Headers, "Connection", "keep-alive");
            if (url.Contains("mms.pinduoduo.com"))
            {
                request.Headers.Add("Origin", "https://mms.pinduoduo.com");
                request.Referer = getReferer(url);
            }
            else if (url.Contains("yingxiao"))
            {
                request.Headers.Add("Origin", "https://yingxiao.pinduoduo.com");
                request.Referer = getReferer(url);
                request.Headers.Add("Etag", "ob18wux5kf2ktu");
            }

            request.Headers.Add("Cookie", StrCookie + "x-visit-time=" + getvisittime() + ";mms_b84d1838=3523,321,3254,3532,3571,3474,3475,3477,3479,3482,1202,1203,1204,1205,3417,3397");


            //流式发起请求
            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(dataBytes, 0, dataBytes.Length);
            }

            string PASSID = string.Empty;
            // 流式获取数据响应
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            {
                string challenge = null;
                string NewStrCookie = string.Empty;
                challenge = response.GetResponseHeader("Set-Cookie");
                if (challenge != null && !string.IsNullOrWhiteSpace(challenge))
                    PASSID = challenge;
                if (!string.IsNullOrWhiteSpace(PASSID))
                {

                    string[] cookieParts = StrCookie.Split(';');
                    string[] keyValue = cookieParts[0].Split('=');
                    foreach (string part in cookieParts[0..])
                    {
                        if (!string.IsNullOrWhiteSpace(part))
                        {
                            string[] attribute = part.Split('=');
                            string attributeName = attribute[0].Trim();
                            string attributeValue = attribute[1].Trim();
                            if (attributeName == "PASSID")
                            {
                                if (!PASSID.Contains("uid"))
                                {
                                    NewStrCookie += PASSID.Split('=')[0] + ";";
                                }

                            }
                            if (attributeName == "x-visit-time")
                            {
                            }
                            else
                            {
                                NewStrCookie += attributeName + "=" + attributeValue + ";";
                            }

                        }
                    }
                    if (!string.IsNullOrWhiteSpace(NewStrCookie))
                    {
                        //if (StoreId > 0)
                        //{
                        //    var StoreRep2 = App.GetService<StoreRepository>();
                        //    bool Result = StoreRep2.UpdateSetColumnsTrue(it => new storemanagement
                        //    {
                        //        CookieStr = NewStrCookie,
                        //        UpdateTime = DateTime.Now,
                        //    }, it => it.Id == StoreId);
                        //}


                    }
                }


                StringBuilder sb = new StringBuilder();
                char[] buffer = new char[1024];
                int bytesRead;

                // 循环获取流式数据
                while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, bytesRead);
                    Console.WriteLine(buffer);
                    buffer = new char[1024];
                }

                return NewStrCookie;
            }


        }
        catch (Exception e)
        {

            return e.Message;
        }
        finally { }
    }



    /// <summary>
    /// 传入Cookie 动态变更服务器时间
    /// </summary>
    /// <param name="Cookie"></param>
    /// <returns></returns>
    public static string getNewCookie(string Cookie)
    {
        string NewCookie = string.Empty;
        DateTime now = DateTime.Now;
        long milliseconds = now.Ticks / 10000;
        if (!Cookie.Contains("x-visit-time"))
        {
            NewCookie = Cookie + "x-visit-time=" + milliseconds + ";";
        }
        else
        {
            string[] cookieParts = Cookie.Split(';');
            string[] keyValue = cookieParts[0].Split('=');
            foreach (string part in cookieParts[0..])
            {
                if (!string.IsNullOrWhiteSpace(part))
                {
                    string[] attribute = part.Split('=');
                    string attributeName = attribute[0].Trim();
                    string attributeValue = attribute[1].Trim();
                    if (attributeName == "x-visit-time")
                    {
                        attributeValue = Convert.ToString(milliseconds);
                    }
                    NewCookie += attributeName + "=" + attributeValue + ";";
                }
            }
        }
        return NewCookie;
    }




    /// <summary>
    /// 字典转string
    /// </summary>
    /// <param name="dict"></param>
    /// <returns></returns>
    public static string DictionaryToStringData(Dictionary<string, string> dict)
    {
        string strTemp = string.Empty;
        foreach (var item in dict)
        {
            strTemp += item.Key + "=" + item.Value + ";";
        }
        return strTemp.Trim(';');
    }

    public static string DictionaryToStringData(Dictionary<string, object> dict)
    {
        string strTemp = string.Empty;
        foreach (var item in dict)
        {
            strTemp += item.Key + "=" + item.Value + ";";
        }
        return strTemp.Trim(';');
    }

    public static string getvisittime()
    {
        DateTime now = DateTime.Now;
        long milliseconds = now.Ticks / 10000;
        return milliseconds.ToString();
    }


    public static string getvisittime2()
    {
        TimeSpan mTimeSpan = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
        //得到精确到秒的时间戳（长度10位）
        long time = (long)mTimeSpan.TotalSeconds;
        return time.ToString();
    }


    /// <summary>
    /// 获取Referer
    /// </summary>
    /// <param name="Url"></param>
    /// <returns></returns>
    public static string getReferer(string Url)
    {
        string Referer = "https://mms.pinduoduo.com";
        if (Url.Contains("commonMallInfo"))
        {
            Referer = "https://mms.pinduoduo.com/mallcenter/changeAccountInfo/accountSetting/accountInfo";
        }
        else if (Url.Contains("goodsList"))
        {
            Referer = "https://mms.pinduoduo.com/goods/goods_list_win";
        }
        else if (Url.Contains("checkLogin") || (Url.Contains("generateAccessToken")))
        {
            Referer = "https://mms.pinduoduo.com/home/";
        }
        else if (Url.Contains("recentOrderList"))
        {
            Referer = "https://mms.pinduoduo.com/orders/list";
        }
        else if (Url.Contains("batchOnSale") ||
            Url.Contains("batchOffSale") ||
            Url.Contains("presale/batch/modify") ||
            Url.Contains("goods/delete/batch") ||
            Url.Contains("goods/delete") ||
            Url.Contains("pc/onSale") ||
            Url.Contains("goods/recover/batch") ||
            Url.Contains("commit/create_by_sn"))
        {
            Referer = "https://mms.pinduoduo.com/goods/goods_list?activeKeyNew=key_4";
        }
        else if (Url.Contains("batch_edit/submit"))
        {
            Referer = "https://mms.pinduoduo.com/goods/goods_list/batch_change";
        }
        else if (Url.Contains("/mains/promotionOverview"))
        {
            Referer = "https://mms.pinduoduo.com/login/sso?redirectUrl=https%3A%2F%2Fyingxiao.pinduoduo.com%2Fmains%2FpromotionOverview&platform=yingxiao&accessType=auto";
        }
        else if (Url.Contains("mms-gateway/user/checkToken"))
        {
            Referer = "https://yingxiao.pinduoduo.com/mains/guide";
        }
        else if (Url.Contains("activity/marketing/stage/batchCreate"))
        {
            Referer = "https://mms.pinduoduo.com/tool/promotion/create?tool_full_channel=10323_87765";
        }
        else if (Url.Contains("templar/api/bill"))
        {
            Referer = "https://cashier.pinduoduo.com/main/bills?__app_code=113";
        }
        else
        {
            Referer = "https://mms.pinduoduo.com/home/";
        }
        return Referer;
    }
}
